Conditions | 6 |
Total Lines | 101 |
Code Lines | 83 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import React from "react"; |
||
27 | |||
28 | export default function ScooterModal({navigation, scooter, modalVisible, setModalVisible, currentCity, setJourneyModal, setToggleTimer, position, setCurrentScooter}) { |
||
29 | const [scooterName, setScooterName] = useState(null); |
||
30 | const [scooterNumber, setScooterNumber] = useState(null); |
||
31 | const [battery, setBattery] = useState(null); |
||
32 | const [fixedRate, setFixedRate] = useState(null); |
||
33 | const [timeRate, setTimeRate] = useState(null); |
||
34 | const [scooterId, setScooterId] = useState(null); |
||
35 | const [scooterPosition, setScooterPosition] = useState(null); |
||
36 | // const [currentScooter, setCurrentScooter] = useState(null); |
||
37 | |||
38 | |||
39 | async function getScooterInfo(): Promise<void> { |
||
40 | if (scooter) { |
||
41 | const title = scooter['name'].split('#'); |
||
42 | const getScooter = await scooterModel.getSpecificScooter(scooter['_id']); |
||
43 | setScooterName(title[0]); |
||
44 | setScooterNumber(title[1]); |
||
45 | setBattery(getBattery(scooter['battery'])); |
||
46 | setScooterId(scooter['_id']); |
||
47 | setScooterPosition(scooter['coordinates']); |
||
48 | setFixedRate(currentCity['taxRates']['fixedRate']); |
||
49 | setTimeRate(currentCity['taxRates']['timeRate']); |
||
50 | setCurrentScooter(getScooter['scooter']) |
||
51 | } |
||
52 | } |
||
53 | |||
54 | useEffect(() => { |
||
55 | const interval = setInterval(() => { |
||
56 | |||
57 | modalVisible ? getScooterInfo() : null; |
||
58 | |||
59 | }, 100); |
||
60 | return () => clearInterval(interval); |
||
61 | }); |
||
62 | |||
63 | |||
64 | |||
65 | async function startJourney() { |
||
66 | const result = await scooterModel.startScooter(scooterId, position, scooterPosition); |
||
67 | setToggleTimer(true); |
||
68 | |||
69 | if (Object.prototype.hasOwnProperty.call(result, 'errors')) { |
||
70 | showMessage({ |
||
71 | message: result['errors']['title'], |
||
72 | type: 'danger', |
||
73 | position: 'center' |
||
74 | }) |
||
75 | |||
76 | return; |
||
77 | }; |
||
78 | |||
79 | showMessage({ |
||
80 | message: result['message'], |
||
81 | type: 'success', |
||
82 | }); |
||
83 | setModalVisible(!modalVisible); |
||
84 | setJourneyModal(true); |
||
85 | }; |
||
86 | |||
87 | return ( |
||
88 | <GestureRecognizer |
||
89 | style={{flex: 1}} |
||
90 | onSwipeDown={ () => setModalVisible(false) } |
||
91 | > |
||
92 | <Modal |
||
93 | animationType="slide" |
||
94 | transparent={true} |
||
95 | visible={modalVisible} |
||
96 | onRequestClose={() => { |
||
97 | setModalVisible(!modalVisible); |
||
98 | }} |
||
99 | |||
100 | > |
||
101 | <View style={styles.modalContainer}></View> |
||
102 | |||
103 | |||
104 | <View style={styles.modalMessage}> |
||
105 | <View style={styles.swipeButton}></View> |
||
106 | |||
107 | <View style={styles.titleContainer}> |
||
108 | <Image style={styles.scooterImage} source={require('../../assets/scooter2.png')}></Image> |
||
109 | |||
110 | <View style={styles.textContainer}> |
||
111 | <Text style={styles.scooterTitle}> {scooterName} {scooterNumber}</Text> |
||
112 | <Image style={styles.battery} source={batteryImages[`${battery}`]}></Image> |
||
113 | </View> |
||
114 | |||
115 | </View> |
||
116 | |||
117 | <Pressable style={styles.tourButton} onPress={() => { |
||
118 | startJourney(); |
||
119 | }}> |
||
120 | <Text style={{color: 'white'}}>Start tour</Text> |
||
121 | </Pressable> |
||
122 | |||
123 | <Text style={{color: 'grey'}}> {fixedRate} kr unlocking + {timeRate} kr / min. </Text> |
||
124 | |||
125 | </View> |
||
126 | </Modal> |
||
127 | </GestureRecognizer> |
||
128 | ) |
||
191 | }) |